package com.fyzk.activity.user.redpacket;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.bean.RedPacketGive;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by miao on 2017/4/26.
 */
@ContentView(R.layout.fg_redpacket)
public class GiveRedpacketFragment extends RedpacketFragment {

    int page = 1;


    @ViewInject(R.id.redpacket_list)
    ListView redpacket_list;
    @ViewInject(R.id.null_record)
    TextView null_record;



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        return x.view().inject(this, inflater, container);//xutil注解;
    }


    ArrayList<RedPacketGive> redPacketGives;


    @Override
    public void onResume() {
        super.onResume();
        requestReceiveRedpacketRecord(page);
    }

    public void requestReceiveRedpacketRecord(int page) {
        try {
            JSONObject request_json = new JSONObject();
            request_json.put("pageIndex", page + "");
            request_json.put("maxResult", 50 + "");
            httpPostJson(CommandUtil.FINDUSERGIVEREDPACKAGEPROCESS, Commands
                    .FINDUSERGIVEREDPACKAGEPROCESS, request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    class RedPacketAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return redPacketGives.size();
        }

        @Override
        public RedPacketGive getItem(int position) {
            return redPacketGives.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = View.inflate(getContext(), R.layout.item_redpacketgive,null);
                holder.amount = (TextView) convertView.findViewById(R.id.amount);
                holder.phone = (TextView) convertView.findViewById(R.id.phonenum);
                holder.date = (TextView) convertView.findViewById(R.id.date);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            RedPacketGive redPacketGive = getItem(position);
            String redpacket = "红包彩金：<font color=\"#ef3338\">"+ PublicMethod.formatMoney(redPacketGive.getAmount()/1d)+"米</font>";
            holder.amount.setText(Html.fromHtml(redpacket));
            holder.date.setText(redPacketGive.getGiveTime());
            holder.phone.setText(redPacketGive.getReceivePhone());

            return convertView;
        }


        class ViewHolder{
            TextView phone;
            TextView date;
            TextView amount;
        }
    }

    @Override
    public void onFailure(int requestCode, String errorcode, String msg) {
        if(!errorcode.equals("900003")){
            showToast(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        if(resultBean.getErrorcode().equals("0")){
            try {
                JSONObject jsonObject = new JSONObject(resultBean.getData());
                redPacketGives = new ArrayList<>();
                redPacketGives.addAll(JSON.parseArray(jsonObject.getString("orders"),RedPacketGive.class));
                if(redPacketGives.size()>0){
                    redpacket_list.setAdapter(new RedPacketAdapter());
                    null_record.setVisibility(View.GONE);
                }else{
                    null_record.setVisibility(View.VISIBLE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }
}
